// pages/address-position/add-posi.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";


const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;

qqmapsdk = new QQMapWX({
  key: 'GQCBZ-FUGO5-ZXQII-QU65G-7IRP2-SUFNN'
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputvalue:"",
    poslist:"",
    selectadd:0,
    markers:"",
    bianji:'',
    xinzeng:'',
    currentcityindex:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    var currentcity=options.currentcity
    var multiIndex = options.multiIndex

    var xuanzes = options.xuanzes
    var xinzeng = options.xinzeng

    if(options.bianji){
      this.setData({
        bianji: options.bianji
      })
    }
    if(options.xinzeng){
      this.setData({
        xinzeng:options.xinzeng
      })
    }
    this.setData({
      currentcity: currentcity,
      multiIndex: multiIndex,
    })
    
  },
  toaddaddress:function(e){
    var item = e.currentTarget.dataset.item;
    console.log(item)

    item.multiIndex = this.data.multiIndex
    if(this.data.xinzeng){
      wx.redirectTo({
        url: '/pages/xin-address/address?item=' + JSON.stringify(item),
      })
    }
    if (this.data.bianji){
      wx.redirectTo({
        url: '/pages/editaddress/editaddress?item=' + JSON.stringify(item),
      })
    }
  },
  getinput:function(e){
    var inputvalue=e.detail.value
    this.setData({
      inputvalue: inputvalue
    })
  },
  tosearch:function(){
    this.getposlist()
  },

  getposlist:function(){
    qqmapsdk.getSuggestion({
      keyword: this.data.inputvalue,
      region: this.data.currentcity,
      success: (res) => {
        // console.log(res)
        this.setData({
          poslist:res.data
        })
        var markers=[]
        for (var x in res.data){
          var obj={
            iconPath: "/pages/images/tuijian.png",
            id: 0,
            latitude: res.data[x].location.lat,
            longitude: res.data[x].location.lng,
            width: 17,
            height: 17
          }
          markers.push(obj)
        }
        this.setData({
          markers: markers
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})